package cn.gf.dimanagementchartdatatransfor.task;

import cn.gf.dimanagementchartdatatransfor.entity.po.*;
import cn.gf.dimanagementchartdatatransfor.service.*;
import cn.gf.dimanagementchartdatatransfor.util.LotNoConvertUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Component
@EnableScheduling
@Slf4j
@EnableAsync
public class DataTransforTask {
	private MasterCdRecordService masterCdRecordService;
	private MasterBtRecordService masterBtRecordService;
	private MasterUsersService masterUsersService;
	private KKTaFmp0030Service kkTaFmp0030Service;
	private ManagementChartCdWgTableService managementChartCdWgTableService;
	private ManagementChartCdCdTableService managementChartCdCdTableService;
	private ManagementChartBtCdTableService managementChartBtCdTableService;


	@Autowired
	public void setMasterCdRecordService(MasterCdRecordService masterCdRecordService) {
		this.masterCdRecordService = masterCdRecordService;
	}
	@Autowired
	public void setKkTaFmp0030Service(KKTaFmp0030Service kkTaFmp0030Service) {
		this.kkTaFmp0030Service = kkTaFmp0030Service;
	}

	@Autowired
	public void setMasterUsersService(MasterUsersService masterUsersService) {
		this.masterUsersService = masterUsersService;
	}
	@Autowired
	public void setManagementChartCdWgTableService(ManagementChartCdWgTableService managementChartCdWgTableService) {
		this.managementChartCdWgTableService = managementChartCdWgTableService;
	}
	@Autowired
	public void setManagementChartCdCdTableService(ManagementChartCdCdTableService managementChartCdCdTableService) {
		this.managementChartCdCdTableService = managementChartCdCdTableService;
	}
	@Autowired
	public void setMasterBtRecordService(MasterBtRecordService masterBtRecordService) {
		this.masterBtRecordService = masterBtRecordService;
	}
	@Autowired
	public void setManagementChartBtCdTableService(ManagementChartBtCdTableService managementChartBtCdTableService) {
		this.managementChartBtCdTableService = managementChartBtCdTableService;
	}

	//USM 测定管理图数据 5分钟运行一次,补60分钟内的数据
	@Scheduled(cron = "0 0/5 * * * ?")
	//@Scheduled(cron = "0/30 * * * * ?")
	@Async("myAsyncThreadPool")
	void task01(){
		// 1.查询N分钟前的数据list
		LambdaQueryWrapper<MasterCdRecord> wrapper1 = new LambdaQueryWrapper<>();
		wrapper1.ge(MasterCdRecord::getUpdateTime, DateUtil.offset(DateUtil.date(), DateField.MINUTE, -60))
		//wrapper1.ge(MasterCdRecord::getUpdateTime, DateUtil.offset(DateUtil.date(), DateField.MINUTE, -600))
				.eq(MasterCdRecord::getLotEndFlag,true);
		List<MasterCdRecord> list = masterCdRecordService.list(wrapper1);
		if(CollUtil.isEmpty(list)) return;
		// 1.1 查询所有人名
		List<MasterUsers> userList = masterUsersService.list();

		//2.组织要插入的数据
		List<ManagementChartCdWgTable> insertList= CollUtil.list(false);
		List<ManagementChartCdCdTable> insertList2= CollUtil.list(false);
		CollUtil.forEach(list.iterator(),(item,index)->{
			// 2.1 测定工程外观数据
			// region
			ManagementChartCdWgTable managementChartCdWgTable = new ManagementChartCdWgTable();
			managementChartCdWgTable.setLOTNO(LotNoConvertUtil.convertString(item.getLotNo()));
			managementChartCdWgTable.setDate_YMD(DateUtil.format(item.getRecordTimeDT(),"yyyyMMdd"));
			managementChartCdWgTable.setDate_HM(DateUtil.format(item.getUpdateTime(),"HHmm"));
			managementChartCdWgTable.setDate_WR(DateUtil.format(item.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
			managementChartCdWgTable.setMaNo(Convert.toInt(item.getMachineNo().replaceAll("[^0-9]", "")).toString());
			managementChartCdWgTable.setZLMANO(Convert.toInt(item.getZlMachineNo().replaceAll("[^0-9]", "")).toString());
			managementChartCdWgTable.setBTMANO(Convert.toInt(item.getBtMachineNo().replaceAll("[^0-9]", "")).toString());
			// 员工姓名
			CollUtil.forEach(userList.iterator(),(user,idx)->{
				if(user.getUserCode().equalsIgnoreCase(item.getUserCode4())){
					managementChartCdWgTable.setWorker(user.getUserFullName());
				}
			});
			if(StrUtil.isEmpty(managementChartCdWgTable.getWorker())) managementChartCdWgTable.setWorker("错误");
			//managementChartTable.setMaTy();  //形名类型
			//managementChartTable.setDiType();
			managementChartCdWgTable.setZpType(item.getProductCode());
			int count1= item.getOkCount1() + item.getNgWtcCount1()+item.getNgCmCount1()+item.getNgC2Count1()+item.getNgC3Count1()+item.getNgC4Count1()+item.getNgC5Count1()+item.getNgWgCount1()+item.getNgJxCount1();
			int count2= item.getOkCount2() + item.getNgWtcCount2()+item.getNgCmCount2()+item.getNgC2Count2()+item.getNgC3Count2()+item.getNgC4Count2()+item.getNgC5Count2()+item.getNgWgCount2()+item.getNgJxCount2();
			int count3= item.getOkCount3() + item.getNgWtcCount3()+item.getNgCmCount3()+item.getNgC3Count3()+item.getNgC3Count3()+item.getNgC4Count3()+item.getNgC5Count3()+item.getNgWgCount3()+item.getNgJxCount3();
			int count4= item.getOkCount4() + item.getNgWtcCount4()+item.getNgCmCount4()+item.getNgC4Count4()+item.getNgC3Count4()+item.getNgC4Count4()+item.getNgC5Count4()+item.getNgWgCount4()+item.getNgJxCount4();
			// 外观不良数
			BigDecimal bigb11= new BigDecimal(Integer.toString(item.getNgWgCount1()*100));
			BigDecimal bigb12= new BigDecimal(Integer.toString(item.getNgWgCount2()*100));
			BigDecimal bigb13= new BigDecimal(Integer.toString(item.getNgWgCount3()*100));
			BigDecimal bigb14= new BigDecimal(Integer.toString(item.getNgWgCount4()*100));
			BigDecimal bigb1Total= new BigDecimal(Integer.toString(item.getNgWgCount1()*100+item.getNgWgCount2()*100+item.getNgWgCount3()*100+item.getNgWgCount4()*100));
			// 标印前(未填充)不良数
			BigDecimal bigb21= new BigDecimal(Integer.toString(item.getNgWtcCount1()*100));
			BigDecimal bigb22= new BigDecimal(Integer.toString(item.getNgWtcCount2()*100));
			BigDecimal bigb23= new BigDecimal(Integer.toString(item.getNgWtcCount3()*100));
			BigDecimal bigb24= new BigDecimal(Integer.toString(item.getNgWtcCount4()*100));
			BigDecimal bigb2Total= new BigDecimal(Integer.toString(item.getNgWtcCount1()*100+item.getNgWtcCount2()*100+item.getNgWtcCount3()*100+item.getNgWtcCount4()*100));

			// 侧面检查不良
			BigDecimal bigb31= new BigDecimal(Integer.toString(item.getNgCmCount1()*100));
			BigDecimal bigb32= new BigDecimal(Integer.toString(item.getNgCmCount2()*100));
			BigDecimal bigb33= new BigDecimal(Integer.toString(item.getNgCmCount3()*100));
			BigDecimal bigb34= new BigDecimal(Integer.toString(item.getNgCmCount4()*100));
			BigDecimal bigb3Total= new BigDecimal(Integer.toString(item.getNgCmCount1()*100+item.getNgCmCount2()*100+item.getNgCmCount3()*100+item.getNgCmCount4()*100));

			//侧2检查不良
			//BigDecimal bigb41= new BigDecimal(Integer.toString(item.getNgC2Count1()*100));
			//BigDecimal bigb42= new BigDecimal(Integer.toString(item.getNgC2Count1()*100));
			//BigDecimal bigb43= new BigDecimal(Integer.toString(item.getNgC2Count1()*100));
			//BigDecimal bigb44= new BigDecimal(Integer.toString(item.getNgC2Count1()*100));
			BigDecimal bigb4Total= new BigDecimal(Integer.toString(item.getNgC2Count1()*100+item.getNgC2Count2()*100+item.getNgC2Count3()*100+item.getNgC2Count4()*100));
			//侧3检查不良
			//BigDecimal bigb51= new BigDecimal(Integer.toString(item.getNgC3Count1()*100));
			//BigDecimal bigb52= new BigDecimal(Integer.toString(item.getNgC3Count1()*100));
			//BigDecimal bigb53= new BigDecimal(Integer.toString(item.getNgC3Count1()*100));
			//BigDecimal bigb54= new BigDecimal(Integer.toString(item.getNgC3Count1()*100));
			BigDecimal bigb5Total= new BigDecimal(Integer.toString(item.getNgC3Count1()*100+item.getNgC3Count2()*100+item.getNgC3Count3()*100+item.getNgC3Count4()*100));
			//侧4检查不良
			//BigDecimal bigb61= new BigDecimal(Integer.toString(item.getNgC4Count1()*100));
			//BigDecimal bigb62= new BigDecimal(Integer.toString(item.getNgC4Count1()*100));
			//BigDecimal bigb63= new BigDecimal(Integer.toString(item.getNgC4Count1()*100));
			//BigDecimal bigb64= new BigDecimal(Integer.toString(item.getNgC4Count1()*100));
			BigDecimal bigb6Total= new BigDecimal(Integer.toString(item.getNgC4Count1()*100+item.getNgC4Count2()*100+item.getNgC4Count3()*100+item.getNgC4Count4()*100));
			//侧5检查不良
			//BigDecimal bigb71= new BigDecimal(Integer.toString(item.getNgC5Count1()*100));
			//BigDecimal bigb72= new BigDecimal(Integer.toString(item.getNgC5Count1()*100));
			//BigDecimal bigb73= new BigDecimal(Integer.toString(item.getNgC5Count1()*100));
			//BigDecimal bigb74= new BigDecimal(Integer.toString(item.getNgC5Count1()*100));
			BigDecimal bigb7Total= new BigDecimal(Integer.toString(item.getNgC5Count1()*100+item.getNgC5Count2()*100+item.getNgC5Count3()*100+item.getNgC5Count4()*100));
			//侧5检查不良
			//BigDecimal bigb81= new BigDecimal(Integer.toString(item.getNgJxCount1()*100));
			//BigDecimal bigb82= new BigDecimal(Integer.toString(item.getNgJxCount1()*100));
			//BigDecimal bigb83= new BigDecimal(Integer.toString(item.getNgJxCount1()*100));
			//BigDecimal bigb84= new BigDecimal(Integer.toString(item.getNgJxCount1()*100));
			BigDecimal bigb8Total= new BigDecimal(Integer.toString(item.getNgJxCount1()*100+item.getNgJxCount2()*100+item.getNgJxCount3()*100+item.getNgJxCount4()*100));


			// 合计投入数
			BigDecimal bigCount1= new BigDecimal(Integer.toString(count1));
			BigDecimal bigCount2= new BigDecimal(Integer.toString(count2));
			BigDecimal bigCount3= new BigDecimal(Integer.toString(count3));
			BigDecimal bigCount4= new BigDecimal(Integer.toString(count4));
			BigDecimal bigCountTotal= new BigDecimal(Integer.toString(count1+count2+count3+count4));

			// 外观不良率
			managementChartCdWgTable.setVIC_DA1(count1==0?0d:(bigb11.divide(bigCount1,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setVIC_DA2(count2==0?0d:(bigb12.divide(bigCount2,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setVIC_DA3(count3==0?0d:(bigb13.divide(bigCount3,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setVIC_DA4(count4==0?0d:(bigb14.divide(bigCount4,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setVIC_AVE(bigCountTotal.intValue()==0?0d:(bigb1Total.divide(bigCountTotal,3, BigDecimal.ROUND_HALF_UP).doubleValue()));
			List<Double> tmpDoubleList1 = Arrays.asList(managementChartCdWgTable.getVIC_DA1(), managementChartCdWgTable.getVIC_DA2(), managementChartCdWgTable.getVIC_DA3(), managementChartCdWgTable.getVIC_DA4());
			managementChartCdWgTable.setVIC_RAN(new BigDecimal(Double.toString(Collections.max(tmpDoubleList1)-Collections.min(tmpDoubleList1))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

			//标印前(未填充)不良率
			managementChartCdWgTable.setBFVIC_DA1(count1==0?0d:(bigb21.divide(bigCount1,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setBFVIC_DA2(count1==0?0d:(bigb22.divide(bigCount2,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setBFVIC_DA3(count1==0?0d:(bigb23.divide(bigCount3,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setBFVIC_DA4(count1==0?0d:(bigb24.divide(bigCount4,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setBFVIC_AVE(bigCountTotal.intValue()==0?0d:(bigb2Total.divide(bigCountTotal,3, BigDecimal.ROUND_HALF_UP).doubleValue()));
			List<Double> tmpDoubleList2 = Arrays.asList(managementChartCdWgTable.getBFVIC_DA1(), managementChartCdWgTable.getBFVIC_DA2(), managementChartCdWgTable.getBFVIC_DA3(), managementChartCdWgTable.getBFVIC_DA4());
			managementChartCdWgTable.setBFVIC_RAN(new BigDecimal(Double.toString(Collections.max(tmpDoubleList2)-Collections.min(tmpDoubleList2))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

			//侧面不良率
			managementChartCdWgTable.setSIDEVIC_DA1(count1==0?0d:(bigb31.divide(bigCount1,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setSIDEVIC_DA2(count1==0?0d:(bigb32.divide(bigCount2,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setSIDEVIC_DA3(count1==0?0d:(bigb33.divide(bigCount3,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setSIDEVIC_DA4(count1==0?0d:(bigb34.divide(bigCount4,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			managementChartCdWgTable.setSIDEVIC_AVE(bigCountTotal.intValue()==0?0d:(bigb3Total.divide(bigCountTotal,3, BigDecimal.ROUND_HALF_UP).doubleValue()));
			List<Double> tmpDoubleList3 = Arrays.asList(managementChartCdWgTable.getSIDEVIC_DA1(), managementChartCdWgTable.getSIDEVIC_DA2(), managementChartCdWgTable.getSIDEVIC_DA3(), managementChartCdWgTable.getSIDEVIC_DA4());
			managementChartCdWgTable.setSIDEVIC_RAN(new BigDecimal(Double.toString(Collections.max(tmpDoubleList3)-Collections.min(tmpDoubleList3))).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

			insertList.add(managementChartCdWgTable);
			//endregion
			//2.2 测定工程测定数据
			//region
			ManagementChartCdCdTable managementChartCdCdTable = new ManagementChartCdCdTable();
			managementChartCdCdTable.setLOTNO(LotNoConvertUtil.convertString(item.getLotNo()));
			managementChartCdCdTable.setDate_YMD(DateUtil.format(item.getRecordTimeDT(),"yyyyMMdd"));
			managementChartCdCdTable.setDate_HM(DateUtil.format(item.getUpdateTime(),"HHmm"));
			managementChartCdCdTable.setDate_WR(DateUtil.format(item.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
			managementChartCdCdTable.setMaNo(Convert.toInt(item.getZlMachineNo().replaceAll("[^0-9]", "")).toString());
			managementChartCdCdTable.setCDMANO(Convert.toInt(item.getMachineNo().replaceAll("[^0-9]", "")).toString());
			// 员工姓名
			CollUtil.forEach(userList.iterator(),(user,idx)->{
				if(user.getUserCode().equalsIgnoreCase(item.getUserCode4())){
					managementChartCdCdTable.setWorker(user.getUserFullName());
				}
			});
			if(StrUtil.isEmpty(managementChartCdCdTable.getWorker())) managementChartCdCdTable.setWorker("错误");
			managementChartCdCdTable.setZpType(item.getProductCode());

			//HOT(侧2)
			managementChartCdCdTable.setDEFR_HOT(bigCountTotal.intValue()==0?0d:(bigb4Total.divide(bigCountTotal,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			//DVF(侧3)
			managementChartCdCdTable.setDEFR_DVF(bigCountTotal.intValue()==0?0d:(bigb5Total.divide(bigCountTotal,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			//DC1(侧4)
			managementChartCdCdTable.setDEFR_DC1(bigCountTotal.intValue()==0?0d:(bigb6Total.divide(bigCountTotal,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			//DC2(侧5)
			managementChartCdCdTable.setDEFR_DC2(bigCountTotal.intValue()==0?0d:(bigb7Total.divide(bigCountTotal,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			//测定合计不良
			BigDecimal ngSumTotal = bigb1Total.add(bigb2Total).add(bigb3Total).add(bigb4Total).add(bigb5Total).add(bigb6Total).add(bigb7Total).add(bigb8Total);
			managementChartCdCdTable.setDEFR_TOTAL(bigCountTotal.intValue()==0?0d:(ngSumTotal.divide(bigCountTotal,2, BigDecimal.ROUND_HALF_UP).doubleValue()));
			insertList2.add(managementChartCdCdTable);

			//endregion
		});

		//3.测定工程外观数据,补充形名相关数据, 并更新数据
		CollUtil.forEach(insertList.iterator(),(item3,idx3)->{
			// 3.1 补充形名相关数据
			LambdaQueryWrapper<KKTaFmp0030> wrapper2 = new LambdaQueryWrapper<>();
			wrapper2.eq(KKTaFmp0030::getProductCode,item3.getZpType());
			KKTaFmp0030 kkTaFmp0030 = kkTaFmp0030Service.getOne(wrapper2);
			if(ObjectUtil.isNotEmpty(kkTaFmp0030)){
				item3.setMaTy(kkTaFmp0030.getMaTy());
				item3.setDiType(kkTaFmp0030.getDiType());
			}
			// 3.2 更新数据
			LambdaQueryWrapper<ManagementChartCdWgTable> wrapper3 = new LambdaQueryWrapper<>();
			wrapper3.eq(ManagementChartCdWgTable::getLOTNO,item3.getLOTNO());
			ManagementChartCdWgTable tmpRecord = managementChartCdWgTableService.getOne(wrapper3);
			if(ObjectUtil.isEmpty(tmpRecord)){
				managementChartCdWgTableService.save(item3);
			}else{
				managementChartCdWgTableService.update(item3,wrapper3);
			}
		});
		//4.测定工程测定数据,补充形名相关数据, 并更新数据
		CollUtil.forEach(insertList2.iterator(),(item4,idx3)->{
			// 4.1 补充形名相关数据
			LambdaQueryWrapper<KKTaFmp0030> wrapper4 = new LambdaQueryWrapper<>();
			wrapper4.eq(KKTaFmp0030::getProductCode,item4.getZpType());
			KKTaFmp0030 kkTaFmp0030 = kkTaFmp0030Service.getOne(wrapper4);
			if(ObjectUtil.isNotEmpty(kkTaFmp0030)){
				item4.setMaTy(kkTaFmp0030.getMaTy());
				item4.setDiType(kkTaFmp0030.getDiType());
			}
			// 4.2 更新数据
			LambdaQueryWrapper<ManagementChartCdCdTable> wrapper5 = new LambdaQueryWrapper<>();
			wrapper5.eq(ManagementChartCdCdTable::getLOTNO,item4.getLOTNO());
			ManagementChartCdCdTable tmpRecord2 = managementChartCdCdTableService.getOne(wrapper5);
			if(ObjectUtil.isEmpty(tmpRecord2)){
				managementChartCdCdTableService.save(item4);
			} else {
				managementChartCdCdTableService.update(item4,wrapper5);
			}
		});

	}

	// USM 半田管理图数据 5分钟运行一次,补60分钟内的数据
	@Scheduled(cron = "0 3/5 * * * ?")
	//@Scheduled(cron = "0/30 * * * * ?")
	@Async("myAsyncThreadPool")
	void task02(){
		// 1.查询N分钟前的数据list
		LambdaQueryWrapper<MasterBtRecord> wrapper1 = new LambdaQueryWrapper<>();
		wrapper1.ge(MasterBtRecord::getUpdateTime, DateUtil.offset(DateUtil.date(), DateField.MINUTE, -60))
				.isNotNull(MasterBtRecord::getUserCode)
				.eq(MasterBtRecord::getLotEndFlag,true)
				.orderByAsc(MasterBtRecord::getUpdateTime);
		List<MasterBtRecord> list = masterBtRecordService.list(wrapper1);
		if(CollUtil.isEmpty(list)) return;
		// 1.1 查询所有人名
		List<MasterUsers> userList = masterUsersService.list();

		// 2.组织要插入的数据
		List<ManagementChartBtCdTable> insertList =CollUtil.list(false);
		CollUtil.forEach(list.iterator(),(item,index)->{
			ManagementChartBtCdTable managementChartBtCdTable = new ManagementChartBtCdTable();
			managementChartBtCdTable.setLOTNO(LotNoConvertUtil.convertString(item.getLotNo()));
			managementChartBtCdTable.setDate_YMD(DateUtil.format(item.getRecordTimeDT(),"yyyyMMdd"));
			managementChartBtCdTable.setDate_HM(DateUtil.format(item.getUpdateTime(),"HHmm"));
			managementChartBtCdTable.setDate_WR(DateUtil.format(item.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
			//managementChartTable.setMaTy();  //形名类型
			managementChartBtCdTable.setMaNo(Convert.toInt(item.getMachineNo().replaceAll("[^0-9]", "")).toString());
			managementChartBtCdTable.setZlMa(Convert.toInt(item.getZlMachineNo().replaceAll("[^0-9]", "")).toString());
			// 员工姓名
			CollUtil.forEach(userList.iterator(),(user,idx)->{
				if(user.getUserCode().equalsIgnoreCase(item.getUserCode())){
					managementChartBtCdTable.setWorker(user.getUserFullName());
				}
			});
			managementChartBtCdTable.setZpType(item.getProductCode());
			double goCount = item.getOkCount1()+item.getOkCount2()+item.getOkCount3()+item.getOkCount4();

			managementChartBtCdTable.setGO_NUM(goCount);
			managementChartBtCdTable.setVIC_DEF1(Convert.toDouble(item.getNgCount1()));
			managementChartBtCdTable.setVIC_DEF2(Convert.toDouble(item.getNgCount2()));
			managementChartBtCdTable.setVIC_DEF3(Convert.toDouble(item.getNgCount3()));
			managementChartBtCdTable.setVIC_DEF4(Convert.toDouble(item.getNgCount4()));
			BigDecimal bigGoCount = new BigDecimal(goCount);
			BigDecimal bigGoCountDiv4 = new BigDecimal(goCount/4);
			BigDecimal bigNgCount1 = new BigDecimal(Integer.toString(item.getNgCount1()));
			BigDecimal bigNgCount2 = new BigDecimal(Integer.toString(item.getNgCount2()));
			BigDecimal bigNgCount3 = new BigDecimal(Integer.toString(item.getNgCount3()));
			BigDecimal bigNgCount4 = new BigDecimal(Integer.toString(item.getNgCount4()));
			BigDecimal bigNgCount = new BigDecimal(item.getNgCount1()+item.getNgCount2()+item.getNgCount3()+item.getNgCount4());
			BigDecimal bigTotal = new BigDecimal(bigGoCount.add(bigNgCount).toString());

			managementChartBtCdTable.setVIC_DEFR1(item.getNgCount1()==0?0d:(bigNgCount1.divide(bigGoCountDiv4,5,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(Integer.toString(100))).doubleValue()));
			managementChartBtCdTable.setVIC_DEFR2(item.getNgCount2()==0?0d:(bigNgCount2.divide(bigGoCountDiv4,5,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(Integer.toString(100))).doubleValue()));
			managementChartBtCdTable.setVIC_DEFR3(item.getNgCount3()==0?0d:(bigNgCount3.divide(bigGoCountDiv4,5,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(Integer.toString(100))).doubleValue()));
			managementChartBtCdTable.setVIC_DEFR4(item.getNgCount4()==0?0d:(bigNgCount4.divide(bigGoCountDiv4,5,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(Integer.toString(100))).doubleValue()));
			//managementChartBtCdTable.setVIC_DEFRX(bigNgCount.divide(bigTotal,5,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(Integer.toString(100))).divide(new BigDecimal(Integer.toString(4)),3,BigDecimal.ROUND_HALF_UP).doubleValue());
			managementChartBtCdTable.setVIC_DEFRX(bigNgCount.divide(bigTotal,5,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(Integer.toString(100))).doubleValue());

			List<Double> tmpNgList= Arrays.asList(managementChartBtCdTable.getVIC_DEFR1(),managementChartBtCdTable.getVIC_DEFR2(),managementChartBtCdTable.getVIC_DEFR3(),managementChartBtCdTable.getVIC_DEFR4());
			//managementChartBtCdTable.setVIC_DEFRR((new BigDecimal(Double.toString(CollUtil.max(tmpNgList)-CollUtil.min(tmpNgList))).doubleValue()));
			managementChartBtCdTable.setVIC_DEFRR((new BigDecimal(CollUtil.max(tmpNgList).toString()).subtract(new BigDecimal(CollUtil.min(tmpNgList).toString())).doubleValue()));
			managementChartBtCdTable.setGM_VAL(Convert.toDouble(item.getYmcSupply()));
			managementChartBtCdTable.setGM_PL(Convert.toDouble(item.getYmcBatch()/1000));
			// 补充形名类型
			//LambdaQueryWrapper<KKTaFmp0030> wrapper2 = new LambdaQueryWrapper<>();
			//wrapper2.eq(KKTaFmp0030::getProductCode,item.getProductCode());
			//KKTaFmp0030 kkTaFmp0030 = kkTaFmp0030Service.getOne(wrapper2);
			//if(ObjectUtil.isNotEmpty(kkTaFmp0030)){
			//	managementChartBtCdTable.setMaTy(kkTaFmp0030.getDiType());
			//}
			managementChartBtCdTable.setMaTy("USM");    // 固定值
			insertList.add(managementChartBtCdTable);
		});
		// 3.插入或更新数据
		CollUtil.forEach(insertList.iterator(),(item3,index3)->{
			LambdaQueryWrapper<ManagementChartBtCdTable> wrapper3 = new LambdaQueryWrapper<>();
			wrapper3.eq(ManagementChartBtCdTable::getLOTNO,item3.getLOTNO());
			if(ObjectUtil.isEmpty(managementChartBtCdTableService.getOne(wrapper3))){
				managementChartBtCdTableService.save(item3);
			}else {
				managementChartBtCdTableService.update(item3,wrapper3);
			}
		});
	}

}
